

#include <sdk_llist.h>

#include <stdio.h>
#include <string.h>

unsigned short sdk_nums[] = {SDK_1,  SDK_2,  SDK_3,  SDK_4,
			     SDK_5,  SDK_6,  SDK_7,  SDK_8,
			     SDK_9,  SDK_10, SDK_11, SDK_12,
			     SDK_13, SDK_14, SDK_15, SDK_16};


int sdk_llist_size(sdk_llist_t *list){

  return list->len;

}

int sdk_llist_contains(sdk_llist_t *list, unsigned int num){

  if (num > SDK_WIDTH || num < 1) return 0;

  return list->list & sdk_nums[num-1];

}

int sdk_llist_add(sdk_llist_t *list, unsigned int num){

  if (num > SDK_WIDTH || num < 1) return 0;

  list->len++;
  return list->list |= sdk_nums[num-1];

}

int sdk_llist_remove(sdk_llist_t *list, unsigned int num){

  if (num > SDK_WIDTH || num < 1) return 0;

  if (list->list & sdk_nums[num-1])
    list->len--;
  else
    return 0;

  return list->list &= ~sdk_nums[num-1];
  

}

int sdk_llist_lowest_num(sdk_llist_t *list){

  int offset = 0;
  while (!(list->list & sdk_nums[offset++]));
  return offset;

}

sdk_llist_t *sdk_llist_full_list(){

  unsigned short int list_data = 0;

  int i = 1;
  for ( ; i <= SDK_WIDTH; i++){
    //printf("Adding: %d\n", i);
    list_data |= sdk_nums[i-1];
  }

  sdk_llist_t *list = SDK_ALLOC_LLIST();
  memset(list, 0, sizeof(sdk_llist_t));
  list->list = list_data;
  list->len = SDK_WIDTH;

  return list;

}

void sdk_llist_print(sdk_llist_t *list){

  printf("LList address: 0x%08lx\n", (unsigned long int)list);
  printf("  Len: %d\n", SDK_LLIST_LEN(list));
  printf("  Elems:\n");

  unsigned short elem_num;
  unsigned short elem = 0;
  SDK_LLIST_ITERATE(list, elem_num, elem){
    if (elem) printf("    Num: %d\n", elem_num);
  }

}

void sdk_llist_print_lite(sdk_llist_t *list){

  printf("{ ");

  unsigned short elem_num;
  unsigned short elem = 0;
  SDK_LLIST_ITERATE(list, elem_num, elem){
    if (elem) printf("%d ", elem_num);
  }

  printf("}");

}
